package com.huabo.cybermonitor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JsonBean;
import com.huabo.cybermonitor.entity.MonitorModelsolution;
import com.huabo.cybermonitor.entity.MonitorSolution;
import com.huabo.cybermonitor.entity.Staff;
import com.huabo.cybermonitor.service.*;
import com.huabo.cybermonitor.util.ConstClass;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 监控执行-模型执行api接口
 *
 * @createTime 2022/8/13
 */
@RestController
@Slf4j
@Api(value = "监控执行-模型执行", tags = {"监控执行-模型执行所有接口"})
@RequestMapping(value = "/cyber/MxzxController")
@SuppressWarnings("all")
public class MxzxController {

    @Autowired
    TreeService treeService;

    @Autowired
    IMonitorModelService iMonitorModelService;

    @Autowired
    IMonitorSolutionService iMonitorSolutionService;

    @Autowired
    IStaffService iStaffService;

    @Autowired
    IMonitorModelsolutionService iMonitorModelsolutionService;

    @Autowired
    IMonitorSolutionresultService iMonitorSolutionresultService;


    /**
     * 监控执行-模型执行
     *
     * @return
     */
    @GetMapping(value = "/jkzx/modelssolutionmgmt")
    @ApiOperation("监控执行-模型执行-主列表页")
    public JsonBean modelssolutionmgmt(
                                       @ApiParam("orgid") @RequestParam(value="orgid",required = false) String orgid,
                                       @ApiParam(name = "pageNumber", value = "pageNumber") @RequestParam(value="pageNumber",required = false,defaultValue = "1") Integer pageNumber,
                                       @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {

        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        TblStaffUtil staff = null;
        try {
            staff = DealUserToken.parseUserToken(token);
            orgid = StringUtils.isBlank(orgid)? staff.getLinkDetp().getOrgid().toString() : orgid;
            Boolean isSelect = treeService.isSJByOrgId(staff.getLinkDetp().getOrgid().toString(), orgid);
            IPage<MonitorSolution> ip = new Page<>(pageNumber, ConstClass.DEFAULT_SIZE);
            QueryWrapper<MonitorSolution> qw = new QueryWrapper<>();
            qw.eq("type", 3);
            qw.eq("SOLUTIONSTATUS", "启用");
            qw.eq("ORGID", orgid);
            qw.orderByDesc("createdate");
            if (isSelect) {
                qw.eq("ORGID", orgid);
            } else {
                qw.eq("ORGID", staff.getLinkDetp().getOrgid());
            }
            iMonitorSolutionService.page(ip, qw);
            Map<String, Object> mv = new HashMap<>();
            mv.put("pageBean", ip);
            mv.put("orgId", orgid);
            mv.put("tblStaff", iStaffService.getById(staff.getStaffid()));
            return new JsonBean(200, "success", mv);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonBean(-1, "failure", "");
    }

}
